package com.shuda.business.channel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuda.business.channel.entity.DataPoolEntity;
import com.shuda.business.channel.service.DataPoolService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.result.Result;
import com.shuda.common.utils.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 数据池管理控制器类
 */
@RestController
@RequestMapping("/api/data-pool")
@Tag(name = "数据池管理", description = "数据池管理相关接口")
@RequiredArgsConstructor
@Validated
public class DataPoolController {

    private final DataPoolService dataPoolService;

    /**
     * 分页查询数据池列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询数据池列表")
    public Result<PageResult<DataPoolEntity>> getDataPoolPage(@Valid PageDTO pageDTO,
                                                             @RequestParam(required = false) String poolName,
                                                             @RequestParam(required = false) String poolType,
                                                             @RequestParam(required = false) String status) {
        QueryWrapper<DataPoolEntity> queryWrapper = new QueryWrapper<>();
        if (poolName != null && !poolName.trim().isEmpty()) {
            queryWrapper.like("pool_name", poolName);
        }
        if (poolType != null && !poolType.trim().isEmpty()) {
            queryWrapper.eq("pool_type", poolType);
        }
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("create_time");
        
        PageResult<DataPoolEntity> pageResult = dataPoolService.getDataPoolPage(pageDTO, queryWrapper);
        return Result.success(pageResult);
    }

    /**
     * 获取数据池详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取数据池详情")
    public Result<DataPoolEntity> getDataPoolById(@PathVariable Long id) {
        DataPoolEntity pool = dataPoolService.getById(id);
        return Result.success(pool);
    }

    /**
     * 创建数据池
     */
    @PostMapping
    @Operation(summary = "创建数据池")
    public Result<String> createDataPool(@Valid @RequestBody DataPoolEntity pool) {
        dataPoolService.createDataPool(pool);
        return Result.success("数据池创建成功");
    }

    /**
     * 更新数据池
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新数据池")
    public Result<String> updateDataPool(@PathVariable Long id, @Valid @RequestBody DataPoolEntity pool) {
        pool.setId(id);
        dataPoolService.updateDataPool(pool);
        return Result.success("数据池更新成功");
    }

    /**
     * 删除数据池
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除数据池")
    public Result<String> deleteDataPool(@PathVariable Long id) {
        dataPoolService.deleteDataPool(id);
        return Result.success("数据池删除成功");
    }

    /**
     * 批量删除数据池
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除数据池")
    public Result<String> deleteDataPools(@RequestBody List<Long> ids) {
        dataPoolService.deleteDataPools(ids);
        return Result.success("数据池批量删除成功");
    }

    /**
     * 更新数据池状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新数据池状态")
    public Result<String> updateDataPoolStatus(@PathVariable Long id, @RequestParam String status) {
        dataPoolService.updateDataPoolStatus(id, status);
        return Result.success("数据池状态更新成功");
    }

    /**
     * 启用数据池
     */
    @PutMapping("/{id}/enable")
    @Operation(summary = "启用数据池")
    public Result<String> enableDataPool(@PathVariable Long id) {
        dataPoolService.enableDataPool(id);
        return Result.success("数据池启用成功");
    }

    /**
     * 禁用数据池
     */
    @PutMapping("/{id}/disable")
    @Operation(summary = "禁用数据池")
    public Result<String> disableDataPool(@PathVariable Long id) {
        dataPoolService.disableDataPool(id);
        return Result.success("数据池禁用成功");
    }

    /**
     * 清空数据池
     */
    @PostMapping("/{id}/clear")
    @Operation(summary = "清空数据池")
    public Result<String> clearDataPool(@PathVariable Long id) {
        dataPoolService.clearDataPool(id);
        return Result.success("数据池清空成功");
    }

    /**
     * 获取数据池统计信息
     */
    @GetMapping("/{id}/statistics")
    @Operation(summary = "获取数据池统计信息")
    public Result<Object> getDataPoolStatistics(@PathVariable Long id) {
        Object statistics = dataPoolService.getDataPoolStatistics(id);
        return Result.success(statistics);
    }

    /**
     * 获取所有启用的数据池
     */
    @GetMapping("/active")
    @Operation(summary = "获取所有启用的数据池")
    public Result<List<DataPoolEntity>> getActiveDataPools() {
        List<DataPoolEntity> pools = dataPoolService.getActiveDataPools();
        return Result.success(pools);
    }
}
